/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaSimulacaoLexico.java
 *
 * Created on 03/10/2008, 09:22:41
 */

package sintelo.visao;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import sintelo.gerador.GeradorReconhecedores;
import sintelo.metaparsers.lexico.CompiladorLexico;
import sintelo.reconhecedor.Token;
import sintelo.reconhecedor.lexico.AnalisadorLexico;
import sintelo.simulador.EspecificacoesLexicas;
import sintelo.simulador.LexicoLista;

/**
 *
 * @author Projetos
 */
public class TelaSimulacaoLexico extends javax.swing.JFrame {


    private GeradorReconhecedores gerador;
    private DefaultTableModel tableTokens;
    private List<Token> tokens;

    /** Creates new form TelaSimulacaoLexico */
    public TelaSimulacaoLexico(GeradorReconhecedores gerador) {
        this.gerador = gerador;
        initComponents();
        tableTokens = (DefaultTableModel) tblTokens.getModel();
        tokens = new ArrayList<Token>();
        atualizaLista();

        tblTokens.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                int sel = tblTokens.getSelectedRow();
                if(sel != -1){
                    Token t = tokens.get(sel);
                    int start = t.getPosicao();
                    int end = start + t.getTexto().length();
                    txtEntrada.setCaretPosition(start);
                    txtEntrada.moveCaretPosition(end);
                }
            }
        });
    }

    private void atualizaLista() {
        tableTokens.setRowCount(0);
        for(Token t : tokens){
            String tipo = gerador.getToken(t.getNumero());
            Object[] linha = {tipo, t.getTexto(), t.getPosicao()};
            tableTokens.addRow(linha);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTokens = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtEntrada = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        btnSimular = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(250, 275));

        tblTokens.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Texto", "Posição"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblTokens.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblTokens);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Entrada");
        jPanel2.add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jScrollPane2.setPreferredSize(new java.awt.Dimension(250, 94));

        txtEntrada.setColumns(20);
        txtEntrada.setRows(5);
        jScrollPane2.setViewportView(txtEntrada);

        jPanel2.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel2);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        btnSimular.setText("Simular");
        btnSimular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimularActionPerformed(evt);
            }
        });
        jPanel1.add(btnSimular);

        btnSair.setText("Sair");
        btnSair.setRequestFocusEnabled(false);
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jPanel1.add(btnSair);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSimularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimularActionPerformed
        simular();
    }//GEN-LAST:event_btnSimularActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        dispose();
    }//GEN-LAST:event_btnSairActionPerformed

  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSimular;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable tblTokens;
    private javax.swing.JTextArea txtEntrada;
    // End of variables declaration//GEN-END:variables

    private void simular() {
        LexicoLista lexicoLista = null;
        try{
            EspecificacoesLexicas esp = new EspecificacoesLexicas(gerador);
            AnalisadorLexico lexico = new AnalisadorLexico(esp);
            String entrada = txtEntrada.getText();
            lexico.setEntrada(entrada);
            lexicoLista = new LexicoLista(lexico);

            lexicoLista.percorreEntrada();
            tokens = lexicoLista.getTokens();
            atualizaLista();
        }catch(Exception e){
            if(lexicoLista != null){
                tokens = lexicoLista.getTokens();
                atualizaLista();
            }
            Mensagens.alerta(this, e.getMessage(), "Problema na entrada");
        }
        txtEntrada.requestFocus();
    }

}
